#ifndef PROTOCOL_H
#define PROTOCOL_H
#include <stdint.h>
#include <string.h>
#include <map>
#include <string>
#include <vector>
#include <memory>
#include <google/protobuf/message.h>

const static uint32_t kMagicNum = 0x7fd39cad;
const static size_t kHeadBaseSize = 20;
const static size_t kBodyMaxLen = 0xffff;

struct InnerHeader
{
	uint32_t magic_num;
	uint32_t connection_id;
	uint32_t addr;   //客户端的连接IP
};

struct Header
{
	uint32_t magic_number;
	uint16_t version;
	uint16_t command;	//消息ID
	uint16_t service_id;
	uint16_t flag;	//高8位压缩，低8位加密
	uint16_t encrypt_length; //消息体加密长度
	uint16_t body_length;	//消息体解密后的长度
	char extend[6];

	Header()
		:magic_number(kMagicNum),
		version(0),
		command(0),
		service_id(0),
		flag(0),
		encrypt_length(0),
		body_length(0)		
	{
		memset(extend, '\0', 6);
	}

	size_t size()const
	{
		return sizeof(Header);
	}
	
};

typedef std::shared_ptr<Header> HeaderPtr;
typedef std::shared_ptr<google::protobuf::Message> MessagePtr;

google::protobuf::Message* createMessage(const std::string& name);

class Command
{
public:
	Command();
	Command(const HeaderPtr& header);

	std::string Encode();
	std::string Decode(const std::string& buff);

	static bool DecodeHead(const std::string& buff, HeaderPtr& head);

private:
	std::string EncodeHead();

private:
	HeaderPtr		header_;
	MessagePtr		body_;

	static std::map<uint16_t, std::string> command_names_;
};

#endif
